package com.trafficsys.modules.loginAndRegister.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @Author Zhao Bin
 * @Date 2021/9/15 15:52
 * @Version 1.0
 */
@Component
@Data
@PropertySource(value = "classpath:config/application-sms.properties",encoding = "UTF-8")
public class SmsSend {
    //key
    @Value("${aliyun.key}")
    private String key;
    //scret
    @Value("${aliyun.screct}")
    private String screte;
    //短信签名
    @Value("${aliyun.SignName}")
    private String signName;
    //短信模板
    @Value("${aliyun.TemplateCode}")
    private String templateCode;
    /**
     * 发送短信
     * @param phone 接收人电话
     * @param code  验证码
     * @return
     */
    public HashMap<String,Object> send(String phone,String code){
        HashMap<String,Object> map=new HashMap<>();
        //cn-hangzhou 是阿里定义的签名固定值。填写阿里云申请短信签名的key和secret值
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", key, screte);
        /*阿里云提供的短信发送api的近期的相关代码，代码以最新的阿里api提供为准*/
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        /*填写阿里云申请的短信发送需要的相关信息*/
        //短信接收人电话号码
        request.putQueryParameter("PhoneNumbers",phone);
        //短信签名
        request.putQueryParameter("SignName", signName);
        //短信模板
        request.putQueryParameter("TemplateCode", templateCode);
        //短信内容
        request.putQueryParameter("TemplateParam", "{'code':"+code+"}");

            //获取发送短信的响应信息，响应的结果参数请参考阿里云提供的api信息
            //短信发送的响应信息rs(code:(ok即为发送成功)、)
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
            System.err.println(response.getData());
            ObjectMapper mapper=new ObjectMapper();
            HashMap<String,Object> mp=mapper.readValue(response.getData().toString(),HashMap.class);
            if (mp.get("Message").equals("OK")){//从阿里云短信服务器返回的数据  OK代表短信发送成功
                map.put("info","短信发送成功");
            }
        } catch (ClientException e) {
            map.put("info","短信发送失败");
            System.out.println("-------------短信发送失败");
            e.printStackTrace();
        }  catch (JsonProcessingException e) {
            map.put("info","短信发送失败");
            System.out.println("**************短信发送失败");
            e.printStackTrace();
        }
    return map;
    }
}

